package com.project.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.model.DiningTable;
import com.project.server.vo.DiningTableVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface DiningTableMapper extends BaseMapper<DiningTable> {

    /**
     * 获取餐厅的可用餐桌
     */
    @Select("SELECT * FROM dining_table WHERE restaurant_id = #{restaurantId} AND status = 0")
    List<DiningTable> getAvailableTables(Long restaurantId);

    Page<DiningTableVO> getDiningTablePage(Page<DiningTable> page,
                                         @Param("tableNumber") String tableNumber,
                                         @Param("restaurantId") Long restaurantId,
                                         @Param("status") Integer status);

    @Select("SELECT " +
            "dt.id, dt.table_number, dt.capacity, dt.status, dt.restaurant_id, " +
            "CASE WHEN dt.status = 0 THEN true ELSE false END as available " +
            "FROM dining_table dt " +
            "WHERE dt.restaurant_id = #{restaurantId}")
    List<DiningTableVO> getByRestaurantId(@Param("restaurantId") Long restaurantId);




    @Select("SELECT COUNT(*) > 0 FROM orders WHERE status in (0, 1) AND restaurant_id = #{restaurantId} AND table_id = #{tableId} " +
            "AND reservation_time BETWEEN #{startTime} AND #{endTime} ")
    boolean getByReservationTime(LocalDateTime startTime, LocalDateTime endTime, Long restaurantId,Long tableId);



}
